﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class WeightSetForm : Form
    {
        public WeightSetForm()
        {
            InitializeComponent();
        }

        private void WeightSetForm_Load(object sender, EventArgs e)
        {
            int useWay = int.Parse(ConfigurationManager.AppSettings["DCUseWay"]);
            if (useWay == 0)
            {
                chkLeftDC.Checked = true;
                chkRightDC.Checked = false;
            }
            else if (useWay == 1)
            {
                chkLeftDC.Checked = false;
                chkRightDC.Checked = true;
            }
            else
            {
                chkLeftDC.Checked = true;
                chkRightDC.Checked = true;
            }
            this.txtLeftType.Text = ConfigurationManager.AppSettings["LeftDCType"].ToString();
            this.txtLeftName.Text = ConfigurationManager.AppSettings["LeftDCName"].ToString();
            this.txtLeftCOM.Text = ConfigurationManager.AppSettings["LeftDCCom"].ToString();
            this.txtLeftBaud.Text = ConfigurationManager.AppSettings["LeftDCBaud"].ToString();
            this.txtLeftCompareNum.Text = ConfigurationManager.AppSettings["LeftCompareTimes"].ToString();

            this.txtRightType.Text = ConfigurationManager.AppSettings["RightDCType"].ToString();
            this.txtRightName.Text = ConfigurationManager.AppSettings["RightDCName"].ToString();
            this.txtRightCOM.Text = ConfigurationManager.AppSettings["RightDCCom"].ToString();
            this.txtRightBaud.Text = ConfigurationManager.AppSettings["RightDCBaud"].ToString();
            this.txtRightCompareNum.Text = ConfigurationManager.AppSettings["RightCompareTimes"].ToString();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            int useWay = 0;
            if (chkLeftDC.Checked && !chkRightDC.Checked)
                useWay = 0;
            else if (!chkLeftDC.Checked && chkRightDC.Checked)
                useWay = 1;
            else if (chkLeftDC.Checked && chkRightDC.Checked)
                useWay = 2;
            cfa.AppSettings.Settings["DCUseWay"].Value = useWay.ToString();
            cfa.AppSettings.Settings["LeftDCType"].Value = this.txtLeftType.Text.Trim();
            cfa.AppSettings.Settings["LeftDCName"].Value = this.txtLeftName.Text.Trim();
            cfa.AppSettings.Settings["LeftDCCom"].Value = this.txtLeftCOM.Text.Trim();
            cfa.AppSettings.Settings["LeftDCBaud"].Value = this.txtLeftBaud.Text.Trim();
            cfa.AppSettings.Settings["LeftCompareTimes"].Value = this.txtLeftCompareNum.Text.Trim();

            cfa.AppSettings.Settings["RightDCType"].Value = this.txtRightType.Text.Trim();
            cfa.AppSettings.Settings["RightDCName"].Value = this.txtRightName.Text.Trim();
            cfa.AppSettings.Settings["RightDCCom"].Value = this.txtRightCOM.Text.Trim();
            cfa.AppSettings.Settings["RightDCBaud"].Value = this.txtRightBaud.Text.Trim();
            cfa.AppSettings.Settings["RightCompareTimes"].Value = this.txtRightCompareNum.Text.Trim();

            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
